unit EdDocumento;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox, cxCurrencyEdit, cxDBEdit, cxTextEdit, cxMaskEdit,
  cxCalendar, cxLabel, cxDBLabel, cxContainer, cxGroupBox, FMTBcd,
  DBClient, Provider, SqlExpr, cxNavigator, cxDBNavigator,
  cxLookAndFeelPainters, StdCtrls, cxButtons, ExtCtrls, Menus;

type
  TfrmEdDocumento = class(TForm)
    cxGroupBox2: TcxGroupBox;
    lblNumero: TcxDBLabel;
    cxGroupBox4: TcxGroupBox;
    deData: TcxDBDateEdit;
    cxGroupBox11: TcxGroupBox;
    ceCliente: TcxDBCurrencyEdit;
    cxGroupBox1: TcxGroupBox;
    lkLocalCliente: TcxDBLookupComboBox;
    cxGroupBox3: TcxGroupBox;
    cxDBCurrencyEdit1: TcxDBCurrencyEdit;
    cxDBLookupComboBox1: TcxDBLookupComboBox;
    cxGroupBox26: TcxGroupBox;
    cdDespAcessItem: TcxDBCurrencyEdit;
    cxDBCurrencyEdit2: TcxDBCurrencyEdit;
    cxGroupBox6: TcxGroupBox;
    gdItens: TcxGrid;
    gdItensDBTableView1: TcxGridDBTableView;
    colItensCodigo: TcxGridDBColumn;
    colItensDesc: TcxGridDBColumn;
    colItensVlrUnit: TcxGridDBColumn;
    colItensQtd: TcxGridDBColumn;
    colItensVlrTotal: TcxGridDBColumn;
    gdItensLevel1: TcxGridLevel;
    sqlDocumento: TSQLDataSet;
    dsoDocumento: TDataSetProvider;
    cdsDocumento: TClientDataSet;
    dsDocumento: TDataSource;
    dsItens: TDataSource;
    cdsItens: TClientDataSet;
    dspItens: TDataSetProvider;
    sqlItens: TSQLDataSet;
    sqlDocumentoSequencial: TIntegerField;
    sqlDocumentoNumero: TIntegerField;
    sqlDocumentoData: TSQLTimeStampField;
    sqlDocumentoOperacao: TIntegerField;
    sqlDocumentoPessoa: TIntegerField;
    sqlDocumentoValor: TFMTBCDField;
    cdsDocumentoSequencial: TIntegerField;
    cdsDocumentoNumero: TIntegerField;
    cdsDocumentoData: TSQLTimeStampField;
    cdsDocumentoOperacao: TIntegerField;
    cdsDocumentoPessoa: TIntegerField;
    cdsDocumentoValor: TFMTBCDField;
    sqlItensSequencial: TIntegerField;
    sqlItensProduto: TIntegerField;
    sqlItensValorUnit: TFMTBCDField;
    sqlItensQtd: TFMTBCDField;
    sqlItensValorTotal: TFMTBCDField;
    cdsItensSequencial: TIntegerField;
    cdsItensProduto: TIntegerField;
    cdsItensValorUnit: TFMTBCDField;
    cdsItensQtd: TFMTBCDField;
    cdsItensValorTotal: TFMTBCDField;
    sqlPessoas: TSQLDataSet;
    dspPessoas: TDataSetProvider;
    cdsPessoas: TClientDataSet;
    dsPessoas: TDataSource;
    sqlPessoasCodigo: TIntegerField;
    sqlPessoasNome: TStringField;
    cdsPessoasCodigo: TIntegerField;
    cdsPessoasNome: TStringField;
    dsOperacoes: TDataSource;
    cdsOperacoes: TClientDataSet;
    dspOperacoes: TDataSetProvider;
    sqlOperacoes: TSQLDataSet;
    sqlOperacoesCodigo: TIntegerField;
    sqlOperacoesDescricao: TStringField;
    cdsOperacoesCodigo: TIntegerField;
    cdsOperacoesDescricao: TStringField;
    sqlProdutos: TSQLDataSet;
    IntegerField1: TIntegerField;
    StringField1: TStringField;
    dspProdutos: TDataSetProvider;
    cdsProdutos: TClientDataSet;
    IntegerField2: TIntegerField;
    StringField2: TStringField;
    dsProdutos: TDataSource;
    Panel1: TPanel;
    btnConsulta: TcxButton;
    btnSair: TcxButton;
    cxButton2: TcxButton;
    pmCategoria: TPopupMenu;
    Categoria1: TMenuItem;
    sqlItensSeqDoc: TIntegerField;
    cdsItensSeqDoc: TIntegerField;
    sqlItensCategoria: TIntegerField;
    cdsItensCategoria: TIntegerField;
    cxGroupBox5: TcxGroupBox;
    cxDBLookupComboBox2: TcxDBLookupComboBox;
    sqlCategorias: TSQLDataSet;
    dspCategorias: TDataSetProvider;
    cdsCategorias: TClientDataSet;
    dsCategorias: TDataSource;
    sqlCategoriasSequencial: TIntegerField;
    sqlCategoriasDEscricao: TStringField;
    cdsCategoriasSequencial: TIntegerField;
    cdsCategoriasDEscricao: TStringField;
    procedure FormShow(Sender: TObject);
    procedure QueryBasica;
    procedure CarregaItens(iSeq:Integer);
    procedure dsDocumentoDataChange(Sender: TObject; Field: TField);
    procedure cdsDocumentoAfterPost(DataSet: TDataSet);
    procedure cdsDocumentoBeforePost(DataSet: TDataSet);
    procedure CarregaDataSets;
    procedure gdItensDBTableView1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure cdsItensAfterPost(DataSet: TDataSet);
    procedure cdsItensBeforePost(DataSet: TDataSet);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure gdItensDBTableView1DBColumn2PropertiesCloseUp(
      Sender: TObject);
    procedure cdsItensValorUnitValidate(Sender: TField);
    procedure cdsItensQtdValidate(Sender: TField);
    procedure cdsItensAfterDelete(DataSet: TDataSet);
    procedure btnConsultaClick(Sender: TObject);
    procedure cxButton2Click(Sender: TObject);
    procedure btnSairClick(Sender: TObject);
    procedure cdsDocumentoAfterInsert(DataSet: TDataSet);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Categoria1Click(Sender: TObject);
  private
    S:TStringList;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmEdDocumento: TfrmEdDocumento;

implementation
Uses Bib, M_Principal, ListaCategoria;
{$R *.dfm}

procedure TfrmEdDocumento.FormShow(Sender: TObject);
begin
  S:=TStringList.Create;
  CarregaDataSets;
  QueryBasica;
  with cdsDocumento do Begin
    if (frmEdDocumento.Tag > 0) then begin
      Edit;
    end else begin
      Insert;
    end;
  end;
end;

procedure TfrmEdDocumento.QueryBasica;
begin
  S.Clear;
  S.Add('Select Sequencial, Numero, Data, Operacao, Pessoa, Valor');
  S.Add('From Documentos');
  S.Add('Where Sequencial = ' + IntToStr(frmEdDocumento.Tag));
  with cdsDocumento do Begin
    Close;
    CommandText:=S.text;
    Open;
  end;
end;

procedure TfrmEdDocumento.dsDocumentoDataChange(Sender: TObject; Field: TField);
begin
  if cdsDocumento.RecordCount > 0 then begin
    CarregaItens(frmEdDocumento.Tag);
  end;
end;

procedure TfrmEdDocumento.CarregaItens(iSeq: Integer);
begin
  S.Clear;
  S.Add('Select SeqDoc, Sequencial, Produto, ValorUnit, Qtd, ValorTotal, Categoria');
  S.Add('From ItensDocumento');
  S.Add('Where SeqDoc = ' + IntToStr(iSeq));
  with cdsItens do Begin
    Close;
    CommandText:=S.text;
    Open;
  end;
end;

procedure TfrmEdDocumento.cdsDocumentoAfterPost(DataSet: TDataSet);
begin
  cdsDocumento.ApplyUpdates(0);
  frmEdDocumento.Tag:=cdsDocumento.FieldByName('Sequencial').asInteger;
end;

procedure TfrmEdDocumento.cdsDocumentoBeforePost(DataSet: TDataSet);
begin
  if cdsDocumento.State = dsInsert then Begin
    cdsDocumento.FieldByName('Sequencial').asINteger:=CarregaSeq('Documentos','Sequencial');
    frmEdDocumento.Tag:=cdsDocumento.FieldByName('Sequencial').asInteger;
  end;
end;

procedure TfrmEdDocumento.CarregaDataSets;
begin
  cdsPessoas.Open;
  cdsOperacoes.Open;
  cdsProdutos.Open;
  cdsCategorias.Open;
end;

procedure TfrmEdDocumento.gdItensDBTableView1KeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  if not (cdsDocumento.State in [dsEdit,dsInsert]) then begin
    if (key=vk_INSERT) then begin
      colItensCodigo.FocusWithSelection;
      cdsItens.Insert;
    end else if (key=vk_DELETE) then begin
      cdsItens.Delete;
    end;
  end;
end;

procedure TfrmEdDocumento.cdsItensAfterPost(DataSet: TDataSet);
begin
  cdsItens.ApplyUpdates(0);
  QueryBasica;
end;

procedure TfrmEdDocumento.cdsItensBeforePost(DataSet: TDataSet);
begin
  if cdsItens.State = dsInsert then begin
    cdsItens.FieldByName('SeqDoc').asINteger:=cdsDocumento.FieldByName('Sequencial').asINteger;
    cdsItens.FieldByName('Sequencial').asINteger:=CarregaSeq('ItensDocumento','Sequencial');
    cdsItens.FieldByName('Categoria').asInteger:=BuscaConfigOper(cdsItens.FieldByName('Produto').asInteger,cdsDocumento.FieldByName('Operacao').asInteger);
  end;
end;

procedure TfrmEdDocumento.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if activecontrol <> gdItens Then begin
    TrataTecla(Key);
  end;
end;

procedure TfrmEdDocumento.gdItensDBTableView1DBColumn2PropertiesCloseUp(
  Sender: TObject);
begin
  ColItensQtd.FocusWithSelection;
  cdsItens.FieldByName('Qtd').asFloat:=1;
end;

procedure TfrmEdDocumento.cdsItensValorUnitValidate(Sender: TField);
begin
  cdsItens.FieldByName('ValorTotal').asFloat:=cdsItens.FieldByName('ValorUnit').asFloat*cdsItens.FieldByName('Qtd').asFloat;
end;

procedure TfrmEdDocumento.cdsItensQtdValidate(Sender: TField);
begin
  cdsItens.FieldByName('ValorTotal').asFloat:=cdsItens.FieldByName('ValorUnit').asFloat*cdsItens.FieldByName('Qtd').asFloat;
end;

procedure TfrmEdDocumento.cdsItensAfterDelete(DataSet: TDataSet);
begin
  cdsItens.ApplyUpdates(0);
  cdsDocumento.Refresh;
end;

procedure TfrmEdDocumento.btnConsultaClick(Sender: TObject);
begin
  if cdsDocumento.State in [dsInsert,dsEdit] then begin
    cdsDocumento.Post;
  end;
end;

procedure TfrmEdDocumento.cxButton2Click(Sender: TObject);
begin
  if cdsDocumento.State in [dsInsert,dsEdit] then begin
    cdsDocumento.Cancel;
  end;
end;

procedure TfrmEdDocumento.btnSairClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmEdDocumento.cdsDocumentoAfterInsert(DataSet: TDataSet);
begin
  cdsDocumento.FieldByName('Data').asDateTime:=Date;
end;

procedure TfrmEdDocumento.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  If Key = vk_F9 then begin
    btnSairClick(Sender);
  end;
end;

procedure TfrmEdDocumento.Categoria1Click(Sender: TObject);
begin
  Application.CreateForm(TfrmListaCategorias,frmListaCategorias);
  frmListaCategorias.cdsCategorias.Locate('Sequencial', cdsItens.FieldByName('Categoria').asInteger, [loCaseInsensitive]);
  frmListaCategorias.ShowModal;
  if frmListaCategorias.iCat > 0 then begin
    cdsItens.Edit;
    cdsItens.FieldByName('Categoria').asInteger:=frmListaCategorias.iCat;
    cdsItens.ApplyUpdates(0);
  end;
  FreeAndNil(frmListaCategorias);
end;

end.
